﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AuditModel;

namespace AuditWeb.Controllers
{ 
    public class AnnualEventController : Controller
    {
        private AuditDBEntities db = new AuditDBEntities();
        IAnnualEventBL aebl = new AnnualEventBL();

        //
        // GET: /AnnualEvent/
        [Authorize(Roles = "管理员")]
        public ViewResult Index()
        {
            return View(db.AnnualEvents.ToList());
        }
        [Authorize(Roles = "管理员")]
        public ViewResult ActiveEvents()
        {
            List<SelectListItem> events=new List<SelectListItem>();
            foreach(AnnualEvent ae in aebl.GetActiveEvents())
            {
                events.Add(new SelectListItem{Text=ae.Title, Value=ae.ID});
            }
            events.First().Selected=true;
            return View(events);
        }

        //
        // GET: /AnnualEvent/Details/5
        [Authorize(Roles = "管理员")]
        public ViewResult Details(string id)
        {
            AnnualEvent annualevent = db.AnnualEvents.Single(a => a.ID == id);
            return View(annualevent);
        }

        //
        // GET: /AnnualEvent/Create
        [Authorize(Roles = "管理员")]
        public ActionResult Create()
        {
            List<SelectListItem> statuslist = new List<SelectListItem>();
            statuslist.Add(new SelectListItem() { Text = "关闭", Value = "Inactive" });
            statuslist.Add(new SelectListItem() { Text = "激活", Value = "Active" });
            ViewBag.Status = new SelectList(statuslist, "Value", "Text");

            return View();
        } 

        //
        // POST: /AnnualEvent/Create
        [Authorize(Roles = "管理员")]
        [HttpPost]
        public ActionResult Create(AnnualEvent annualevent)
        {
            if (ModelState.IsValid)
            {
                db.AnnualEvents.AddObject(annualevent);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            List<SelectListItem> statuslist = new List<SelectListItem>();
            statuslist.Add(new SelectListItem() { Text = "关闭", Value = "Inactive" });
            statuslist.Add(new SelectListItem() { Text = "激活", Value = "Active" });
            ViewBag.Status = new SelectList(statuslist, "Value", "Text", annualevent.Status);
            return View(annualevent);
        }
        
        //
        // GET: /AnnualEvent/Edit/5
        [Authorize(Roles = "管理员")]
        public ActionResult Edit(string id)
        {
            AnnualEvent annualevent = db.AnnualEvents.Single(a => a.ID == id);
            List<SelectListItem> statuslist = new List<SelectListItem>();
            statuslist.Add(new SelectListItem() { Text = "关闭", Value = "Inactive" });
            statuslist.Add(new SelectListItem() { Text = "激活", Value = "Active" });
            ViewBag.Status = new SelectList(statuslist, "Value", "Text", annualevent.Status);
            return View(annualevent);
        }

        //
        // POST: /AnnualEvent/Edit/5

        [HttpPost]
        [Authorize(Roles = "管理员")]
        public ActionResult Edit(AnnualEvent annualevent)
        {
            if (ModelState.IsValid)
            {
                db.AnnualEvents.Attach(annualevent);
                db.ObjectStateManager.ChangeObjectState(annualevent, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            List<SelectListItem> statuslist = new List<SelectListItem>();
            statuslist.Add(new SelectListItem() { Text = "关闭", Value = "Inactive" });
            statuslist.Add(new SelectListItem() { Text = "激活", Value = "Active" });
            ViewBag.Status = new SelectList(statuslist, "Value", "Text", annualevent.Status);
            return View(annualevent);
        }

        //
        // GET: /AnnualEvent/Delete/5
        [Authorize(Roles = "管理员")]
        public ActionResult Delete(string id)
        {
            AnnualEvent annualevent = db.AnnualEvents.Single(a => a.ID == id);
            return View(annualevent);
        }

        //
        // POST: /AnnualEvent/Delete/5
        [Authorize(Roles = "管理员")]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {            
            AnnualEvent annualevent = db.AnnualEvents.Single(a => a.ID == id);
            db.AnnualEvents.DeleteObject(annualevent);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}